﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

//using Pibby.Core.Persistence;

//using Microsoft.VisualStudio.TestTools.UnitTesting;

//namespace Pibby.Core.Tests
//{
//    [TestClass]
//    public class RepositoryTests
//    {
//        private PodcastRepository _repository;

//        [TestInitialize]
//        public void Setup()
//        {
//            this._repository = new PodcastRepository();
//            this._repository.Path = ":memory:";

//            this._repository.CreateNewDatabase();
//            this._repository.CheckSQLiteDb();
//        }

//        [TestMethod]
//        [ExpectedException (typeof (CannotReadPersistentObjectException))]
//        public void ReadEpisode_UnknownId_ThrowsException()
//        {
//            this._repository.GetEpisodeById("This does not exist");
//        }

//        [TestMethod]
//        [ExpectedException(typeof(CannotReadPersistentObjectException))]
//        public void ReadPodcast_UnknownId_ThrowsException()
//        {
//            this._repository.GetPodcastById("This also does not exist");
//        }

//        [TestMethod]
//        public void Insert_IntoEmptyRepository_PodcastIsAdded()
//        {
//            Podcast p = new Podcast()
//            {
//                Feed = new Uri("http://mypodcast.com"),
//                ImageUrl = null,
//                PublicationDate = DateTime.Today,
//                Name = "My Podcast",
//                Description = "This is a description of this podcast"
//            };

//            Assert.IsFalse(this._repository.PodcastExists(p.Id), "Repository should be empty initially");
//            this._repository.Put(p);
//            Assert.IsTrue(this._repository.PodcastExists(p.Id), "Repository should have single podcast content");

//            Podcast copy = this._repository.GetPodcastByName(p.Name);

//            Assert.IsNotNull(copy);
//        }

//        [TestMethod]
//        public void Insert_IntoEmptyRepository_EpisodeIsAdded()
//        {
//            Episode e = new Episode()
//            {
//                Feed = new Uri("http://mypodcast.com/ep1"),
//                Name = "My First Podcast Episode",
//                Id = "my_first_podcast_episode",
//                FileSize = 1024,
//                Duration = "1:00:09",
//                PodcastName = "My Podcast",
//                PublicationDate = DateTime.Today,
//                Age = EpisodeAge.BrandNew,
//                Stage = EpisodeLifecycle.NotDownloaded,
//                Description = "This is the first episode of our podcast"
//            };

//            Assert.IsFalse(this._repository.EpisodeExists(e.Id), "Repository should be empty initially");
//            this._repository.Put(e);
//            Assert.IsTrue(this._repository.EpisodeExists(e.Id), "Repository should have single episode content");

//            Episode copy = this._repository.GetEpisodeById(e.Id);

//            Assert.IsNotNull(copy);
//            Assert.AreEqual(e.Id, copy.Id);
//        }

//        [TestMethod]
//        public void Insert_IntoEmptyRepository_PodcastMatchesOriginal()
//        {
//            Podcast p = new Podcast()
//            {
//                Feed = new Uri("http://mypodcast.com"),
//                //ImageUrl = null,
//                PublicationDate = DateTime.Today,
//                Name = "My Podcast",
//                Description = "This is a description of this podcast"
//            };

//            this._repository.Put(p);

//            Assert.IsTrue(this._repository.PodcastExists(p.Id), "Repository should have single podcast content");

//            Podcast copy = this._repository.GetPodcastById(p.Id);

//            Assert.AreEqual(p.Name, copy.Name, "Name has changed");
//            Assert.AreEqual(p.PublicationDate, copy.PublicationDate, "LastUpdated has changed");
//            Assert.AreEqual(p.FeedUrl, copy.FeedUrl, "Feed url has changed");
//            Assert.AreEqual(p.Description, copy.Description, "Description has changed");
//            Assert.AreEqual(1, this._repository.GetAllPodcasts().Count, "Should have only one podcast in the repository");
//        }

//        [TestMethod]
//        public void Insert_IntoEmptyRepository_EpisodeMatchesOriginal()
//        {
//            Episode e = new Episode()
//            {
//                Feed = new Uri("http://mypodcast.com/ep1"),
//                Name = "My First Podcast Episode",
//                Id = "my_first_podcast_episode",
//                FileSize = 1024,
//                Duration = "1:00:09",
//                PodcastName = "My Podcast",
//                PublicationDate = DateTime.Today,
//                Age = EpisodeAge.BrandNew,
//                Stage = EpisodeLifecycle.NotDownloaded,
//                Description = "This is the first episode of our podcast"
//            };

//            this._repository.Put(e);

//            Episode copy = this._repository.GetEpisodeById(e.Id);

//            Assert.AreEqual(e.Id, copy.Id, "Ids do not match");
//            Assert.AreEqual(e.Feed, copy.Feed, "Feeds do not match");
//            Assert.AreEqual(e.Name, copy.Name, "Names do not match");
//            Assert.AreEqual(e.FileSize, copy.FileSize, "File Sizes do not match");
//            Assert.AreEqual(e.Duration, copy.Duration, "Durations do not match");
//            Assert.AreEqual(e.PodcastName, copy.PodcastName, "Podcast Names do not match");
//            Assert.AreEqual(e.PublicationDate, copy.PublicationDate, "Publication Dates do not match");
//            Assert.AreEqual(e.Age, copy.Age, "Ages do not match");
//            Assert.AreEqual(e.Stage, copy.Stage, "Stages do not match");
//            Assert.AreEqual(e.Description, copy.Description, "Descriptions do not match");
//        }

//        [TestMethod]
//        public void Update_ExistingPodcast_DateIsUpdatable()
//        {
//            DateTime firstDate = DateTime.Today.AddDays (-7);

//            Podcast p = new Podcast()
//            {
//                Feed = new Uri("http://mypodcast.com"),
//                Description = "Description of Podcast",
//                ImageUrl = null,
//                PublicationDate = firstDate,
//                Name = "My Podcast",
//            };

//            this._repository.Put(p);

//            Podcast copy = this._repository.GetPodcastById(p.Id);

//            Assert.AreEqual(firstDate, copy.PublicationDate, "Last updated date not persisted correctly");

//            // change date...
//            copy.PublicationDate = DateTime.Today;
//            this._repository.Update(copy);
			
//            Podcast dateChanged = this._repository.GetPodcastById(p.Id);
//            Assert.AreEqual(DateTime.Today, dateChanged.PublicationDate, "Last updated date not changed correctly");
//        }

//        [TestMethod]
//        public void Update_ExistingPodcast_ScoreIsUpdatable()
//        {
//            Podcast p = new Podcast()
//            {
//                Feed = new Uri("http://mypodcast.com"),
//                ImageUrl = null,
//                Description = "Description of Podcast",
//                PublicationDate = DateTime.Today,
//                Name = "My Podcast",
//            };

//            this._repository.Put(p);

//            Assert.IsTrue(this._repository.PodcastExists(p.Id), "Repository should have single podcast content");

//            Podcast copy = this._repository.GetPodcastById(p.Id);
//            Assert.AreEqual(0, copy.Whuffie.Score);

//            copy.Whuffie.Esteem(20);

//            this._repository.Update(copy);

//            Podcast secondRead = this._repository.GetPodcastById(p.Id);

//            Assert.AreEqual(20, secondRead.Whuffie.Score, "Whuffie Score not persisted correctly");
//        }

//        [TestMethod]
//        public void Delete_PodcastRemoveEpisodesFlags_RemovesAllEntities()
//        {
//            Podcast p = new Podcast()
//            {
//                Feed = new Uri("http://mypodcast.com"),
//                ImageUrl = null,
//                Description = "Description of Podcast",
//                PublicationDate = DateTime.Today,
//                Name = "My Podcast"
//            };

//            Assert.IsFalse(this._repository.PodcastExists(p.Id), "Repository should be empty initially");

//            this._repository.Put(p);

//            Assert.IsTrue(this._repository.PodcastExists(p.Id), "Repository should have single podcast content");

//            // now add some episodes...
//            EpisodeCollection collection = new EpisodeCollection();

//            for (int i = 0; i < 10; ++i)
//            {
//                Episode e = new Episode()
//                {
//                    Feed = new Uri(string.Format ("http://mypodcast.com/ep{0}", i + 1)),
//                    Name = string.Format ("My {0} Podcast Episode", i + 1),
//                    Id = string.Format("my_{0}_podcast_episode", i + 1),
//                    Description = "Description of this episode",
//                    FileSize = 1024,
//                    Duration = "1:00:09",
//                    PodcastName = p.Name,
//                    PublicationDate = DateTime.Today,
//                    Age = EpisodeAge.BrandNew,
//                    Stage = EpisodeLifecycle.NotDownloaded
//                };
				
//                collection.AddUnique(e);
//            }

//            this._repository.Import(collection);

//            Assert.AreEqual(10, this._repository.GetAllEpisodes (p).Count);
			
//            this._repository.Remove(p, true);

//            Assert.IsFalse(this._repository.PodcastExists(p.Id), "Repository should report podcast not exists after delete");
//            Assert.AreEqual(0, this._repository.GetAllEpisodes(p).Count);
//        }


//        [TestMethod]
//        public void Insert_ExistingEpisode_CannotAddTwice()
//        {
//            Episode e = new Episode()
//            {
//                Feed = new Uri("http://mypodcast.com/ep1"),
//                Name = "My First Podcast Episode",
//                Id = "my_first_podcast_episode",
//                Description = "Description of this episode",
//                FileSize = 1024,
//                Duration = "1:00:09",
//                PodcastName = "My Podcast",
//                PublicationDate = DateTime.Today,
//                Age = EpisodeAge.BrandNew,
//                Stage = EpisodeLifecycle.NotDownloaded
//            };

//            EpisodeCollection collection = new EpisodeCollection();

//            collection.AddUnique(e);

//            // pre-existing values..
//            this._repository.Import (collection);
			
//            Assert.IsTrue(this._repository.EpisodeExists(e.Id), "Repository should have single episode content");

//            // try to add it again...
//            this._repository.Import(collection);
//            Assert.AreEqual(1, this._repository.GetAllEpisodes().Count, "Should only have one episode");
//        }

//        [TestMethod]
//        public void Update_ExistingEpisode_AgeIsUpdatable()
//        {
//            Episode e = new Episode()
//            {
//                Feed = new Uri("http://mypodcast.com/ep1"),
//                Name = "My First Podcast Episode",
//                Id = "my_first_podcast_episode",
//                Description = "Description of this episode",
//                FileSize = 1024,
//                Duration = "1:00:09",
//                PodcastName = "My Podcast",
//                PublicationDate = DateTime.Today,
//                Age = EpisodeAge.BrandNew,
//                Stage = EpisodeLifecycle.NotDownloaded
//            };

//            EpisodeCollection collection = new EpisodeCollection();

//            collection.AddUnique(e);

//            // pre-existing values..
//            this._repository.Import(collection);

//            Assert.IsTrue(this._repository.EpisodeExists(e.Id), "Repository should have single episode content");

//            Episode saved = this._repository.GetEpisodeById(e.Id);

//            Assert.AreEqual(EpisodeAge.BrandNew, saved.Age);

//            saved.Age = EpisodeAge.Newish;

//            this._repository.UpdateStatus(saved);

//            Episode newish = this._repository.GetEpisodeById(e.Id);
//            Assert.AreEqual(EpisodeAge.Newish, newish.Age);

//            newish.Age = EpisodeAge.BackCatalog;
//            this._repository.UpdateStatus(newish);

//            Episode backCatalog = this._repository.GetEpisodeById(e.Id);
//            Assert.AreEqual(EpisodeAge.BackCatalog, backCatalog.Age);
//        }

//        [TestMethod]
//        public void Update_ExistingEpisode_LifeCycleIsUpdatable()
//        {
//            Episode e = new Episode()
//            {
//                Feed = new Uri("http://mypodcast.com/ep1"),
//                Name = "My First Podcast Episode",
//                Id = "my_first_podcast_episode",
//                Description = "Description of this episode",
//                FileSize = 1024,
//                Duration = "1:00:09",
//                PodcastName = "My Podcast",
//                PublicationDate = DateTime.Today,
//                Age = EpisodeAge.BrandNew,
//                Stage = EpisodeLifecycle.NotDownloaded
//            };

//            EpisodeCollection collection = new EpisodeCollection();

//            collection.AddUnique(e);

//            // pre-existing values..
//            this._repository.Import(collection);

//            Assert.IsTrue(this._repository.EpisodeExists(e.Id), "Repository should have single episode content");

//            Episode saved = this._repository.GetEpisodeById(e.Id);

//            Assert.AreEqual(EpisodeLifecycle.NotDownloaded, saved.Stage);

//            saved.Stage = EpisodeLifecycle.Downloaded;
//            this._repository.UpdateStatus(saved);

//            // now should not exist in not downloaded list...
//            //Assert.IsEmpty (this._repository.GetAllEpisodes(EpisodeLifecycle.NotDownloaded));

//            saved = this._repository.GetEpisodeById(e.Id);

//            Assert.AreEqual(EpisodeLifecycle.Downloaded, saved.Stage);

//            saved.Stage = EpisodeLifecycle.Transferred;
//            this._repository.UpdateStatus(saved);

//            //Assert.IsEmpty(this._repository.GetAllEpisodes(EpisodeLifecycle.Downloaded), "Downloaded list should now be empty");

//            var transferred = this._repository.GetAllEpisodes(EpisodeLifecycle.Transferred);

//            Assert.AreEqual(e.Id, transferred[0].Id, "Item has not been marked as transferred");
//        }

//        [TestMethod]
//        public void ApplicationVersion_Update_IsUpdatable()
//        {
//            ApplicationVersion originalVersion = this._repository.GetVersion ();

//            ApplicationVersion updatedVersion = new ApplicationVersion()
//                {
//                    Major = 1,
//                    Minor = 2,
//                    Build = 3,
//                    Revision = 4
//                };

//            this._repository.Update(updatedVersion);

//            ApplicationVersion refresh = this._repository.GetVersion();

//            Assert.AreEqual(updatedVersion.Major, refresh.Major);
//            Assert.AreEqual(updatedVersion.Minor, refresh.Minor);
//            Assert.AreEqual(updatedVersion.Build, refresh.Build);
//            Assert.AreEqual(updatedVersion.Revision, refresh.Revision);
//        }
//    }
//}
